<?php

namespace Modules\Car\Http\Controllers;

use App\Helpers\CommTrait;
use App\Helpers\PayTrait;
use App\Helpers\ResponseData;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\RecognizeDrivingLicense;
use Modules\Car\Entities\Service;
use Modules\Car\Entities\CarOrder;

//车辆维修控制器
class ServiceController extends Controller
{
    use ResponseData,PayTrait,CommTrait;
    public function list(Service $model,Request $request){
        $type = $request->input('type','');
        $car_id = $request->input('car_id','');
        $size = $request->input('size',10);
        if($request->filled('customer_id')){
            $model = $model->where('customer_id',$request->customer_id);
        }
        if($request->filled('is_custom')&&$request->is_custom!=0){
            $model = $model->where('is_custom',$request->is_custom);
        }
        if($request->filled('order_sn')){
            $model = $model->where('order_sn',$request->order_sn);
        }
        if($request->filled('status')){
            $model = $model->where('status',$request->status);
        }
        if($request->filled('search')){
            $name = $request->search;
            $model = $model->where(function($query)use($name){
                $query->where('car_series_name','like','%'.$name.'%')
                    ->orWhere('plate_number','like','%'.$name.'%')
                    ->orWhere('contents','like','%'.$name.'%')
                    ->orWhere('address','like','%'.$name.'%');
            });
        }
        if($type=='all'){

            $data = $model
                ->with('car','sendPerson','series')
                ->when($car_id,function($query)use($car_id){
                    $query->where('car_id',$car_id);
                })
                ->orderBy('id','desc')
                ->get();
        }else{
            $data = $model
                ->with('car','sendPerson','series','order','customer')
                ->when($car_id,function($query)use($car_id){
                    $query->where('car_id',$car_id);
                })
                ->orderBy('id','desc')->paginate($size);
        }

        $arr['uncleared'] = Service::query()->where('is_custom',4)->where('status',4)->where('is_accident_sett',1)->count();
        $arr['repair_price'] = Service::query()->where('id','>',0)->count('repair_price');
        $arr['data']=$data;
        return $this->success($arr);
    }
    public function store(Service $service,Request $request){
        $data = $request->all();
        if(!$request->filled('outage_rate')){
            $data['outage_rate'] = 0.00;
        }
        $service->fill($data);
        $service->save();
        $this->serviceCarStatus($request);
        return $this->success('保存成功');
    }
    public function serviceCarStatus($request){
        //待维修
        if($request->need_start>now()){
            RecognizeDrivingLicense::query()
                ->where('id',$request->car_id)
                ->update(['use_status'=>10]);
        }else{
            if($request->filled('is_custom')&&$request->is_custom==3){
                RecognizeDrivingLicense::query()
                    ->where('id',$request->car_id)
                    ->update(['use_status'=>2,'is_used'=>1]);
            }
            if($request->filled('is_custom')&&$request->is_custom==4){
                RecognizeDrivingLicense::query()
                    ->where('id',$request->car_id)
                    ->update(['use_status'=>2]);
            }
        }
    }
    public function edit(Service $service,Request $request){
        $data = $service->find($request->id);
        return $this->success($data);
    }
    public function update(Service $service,Request $request){
        $data = $service->find($request->id);
        $map = $request->all();
        if($request->filled('end')&&$request->end==1){
            $map['end'] = now();
            $car = RecognizeDrivingLicense::query()
                ->where('id',$data->car_id)->first();
            $usedorders = CarOrder::query()->where('gear_car_id', $car->id)->where('status',5)->whereIn('process_status',[1,2])->count();
            if($data->is_custom == 2){
                $car->repair_status = 1;
                $car->use_status = 1;
                if($usedorders==0){
                    $car->is_used = 0;
                }
                $car->save();
            }
            // 在租中的车辆不改变在租状态
            if($data->is_custom == 3){
                $car->use_status = 1;
                if($usedorders==0){
                    $car->is_used = 0;
                }
                $car->repair_status = 1;
                $car->save();
                // 普通维修支出
                $this->expendStatistatic($data->car_id,$data->repair_price);
            }
            if($data->is_custom == 4){
                $car->repair_status = 1;
                $car->use_status = 1;
                if($usedorders==0){
                    $car->is_used = 0;
                }
                $car->save();
            }
            // 事故维修完成统计
            $this->serviceFinishedStatstatic($data);
        }
        $data->update($map);
        return $this->success('修改成功');
    }
    public function destory(Service $service,Request $request){
        $data = $service->find($request->id);
        $data->delete();
    }
}
